package com.shangcheng.goods.controller;


import com.shangcheng.goods.pojo.Goods;
import com.shangcheng.goods.pojo.Sku;
import com.shangcheng.goods.pojo.Spu;
import com.shangcheng.goods.service.SpuService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/spu")
@CrossOrigin
public class SpuController {
    @Autowired
    private SpuService spuService;

    @PostMapping("/save")
    public Result save(@RequestBody Goods goods) {
        spuService.saveGoods(goods);
        return new Result(true, StatusCode.OK, "保存成功");
    }

    @GetMapping("/{id}")
    public Result<Goods> findGoodsById(@PathVariable Long id) {
        Goods goods = spuService.findGoodsById(id);
        return new Result<Goods>(true, StatusCode.OK, "查询成功", goods);
    }

    @GetMapping(value = "/bg/{page}/{size}")
    public Result<PageInfo> findPageGoods(@PathVariable int page, @PathVariable int size) {
        PageInfo<Goods> goods = spuService.findPageGoods(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", goods);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Goods goods) {
        spuService.saveGoods(goods);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable Long id) {
        spuService.audit(id);
        return new Result(true, StatusCode.OK, "审核成功");
    }

    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable Long id) {
        spuService.pull(id);
        return new Result(true, StatusCode.OK, "下架成功");
    }

    @PutMapping("/put/{id}")
    public Result put(@PathVariable Long id) {
        spuService.put(id);
        return new Result(true, StatusCode.OK, "上架成功");
    }

    @DeleteMapping("/logic/delete/{id}")
    public Result logicDelete(@PathVariable Long id) {
        spuService.logicDelete(id);
        return new Result(true, StatusCode.OK, "逻辑删除成功");
    }

    /***
     * 分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page,
                                     @PathVariable int size) {
        //分页查询
        PageInfo<Spu> pageInfo = spuService.findPage(page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }


    @GetMapping(value = "/search2/{id}/{page}/{size}")
    public Result<PageInfo> findPageById(@PathVariable int id,
                                         @PathVariable int page,
                                         @PathVariable int size) {
        //分页查询
        PageInfo<Spu> pageInfo = spuService.findPageById(id, page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }


    @GetMapping(value = "/findPage")
    public Result<PageInfo> findPageIsMarketable(@RequestParam String isMarketable,
                                                 @RequestParam int page,
                                                 @RequestParam int size) {
        //分页查询
        PageInfo<Spu> pageInfo = spuService.findPageIsMarketable(isMarketable, page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @GetMapping(value = "/findPageByStatus")
    public Result<PageInfo> findPageByStatus(@RequestParam String status,
                                             @RequestParam int page,
                                             @RequestParam int size) {
        //分页查询
        PageInfo<Spu> pageInfo = spuService.findPageByStatus(status, page, size);
        return new Result<PageInfo>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @PostMapping("/addsc")
    public Result addsc(@RequestParam String username,
                        @RequestParam String id) {
        spuService.addSC(username, id);
        return new Result(true, StatusCode.OK, "保存成功");
    }

    @PostMapping("/deletesc")
    public Result deletesc(@RequestParam String id) {
        spuService.deleteSC(id);
        return new Result(true, StatusCode.OK, "删除");
    }
}
